using Windows.System;

namespace MultiAgentsClient.Behaviors;

public class CommandOnInputBoxKeyPressBehavior
{
    public static ICommand GetTextBoxCommand(DependencyObject dependencyObject)
    {
        return (ICommand)dependencyObject.GetValue(TextBoxCommandProperty);
    }

    public static ICommand GetTextChangedCommand(DependencyObject dependencyObject)
    {
        return (ICommand)dependencyObject.GetValue(TextChangedCommandProperty);
    }

    public static void SetTextBoxCommand(DependencyObject dependencyObject, ICommand value)
    {
        dependencyObject.SetValue(TextBoxCommandProperty, value);
    }

    public static void SetTextChangedCommand(DependencyObject dependencyObject, ICommand value)
    {
        dependencyObject.SetValue(TextChangedCommandProperty, value);
    }

    public static readonly DependencyProperty TextChangedCommandProperty =
        DependencyProperty.RegisterAttached(
            "TextChangedCommand",
            typeof(ICommand),
            typeof(CommandOnInputBoxKeyPressBehavior),
            new PropertyMetadata(default(ICommand), OnTextBoxCommandChanged));

    public static readonly DependencyProperty TextBoxCommandProperty =
        DependencyProperty.RegisterAttached(
            "TextBoxCommand",
            typeof(ICommand),
            typeof(CommandOnInputBoxKeyPressBehavior),
            new PropertyMetadata(default(ICommand), OnTextBoxCommandChanged));

    private static void OnTextBoxCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is TextBox textBox)
        {
            textBox.TextChanged -= TextBoxTextChangedHandler;
            textBox.KeyDown -= TextBoxKeyDownHandler;

            textBox.TextChanged += TextBoxTextChangedHandler;
            textBox.KeyDown += TextBoxKeyDownHandler;
        }
    }

    private static void TextBoxKeyDownHandler(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        if (sender is not TextBox textBox)
        {
            return;
        }

        ICommand command = GetTextBoxCommand(textBox);

        e.Handled = true;
        if (e.Key == VirtualKey.Enter)
        {
            command.Execute("Enter");
        }
        else if (e.Key == VirtualKey.Tab)
        {
            command.Execute("Tab");
        }
        else if (e.Key == VirtualKey.Up)
        {
            command.Execute("Up");
        }
        else if (e.Key == VirtualKey.Down)
        {
            command.Execute("Down");
        }
        else if (e.Key == VirtualKey.Shift)
        {
            command.Execute("Shift");
        }
        else if (e.Key == VirtualKey.Control)
        {
            command.Execute("Control");
        }
        else if (e.Key == VirtualKey.V)
        {
            command.Execute("V");
        }
        else
        {
            e.Handled = false;
        }
    }

    private static void TextBoxTextChangedHandler(object sender, TextChangedEventArgs e)
    {
        if (sender is not TextBox textBox)
        {
            return;
        }

        ICommand command = GetTextBoxCommand(textBox);
        command.Execute("TextChange");
    }
}
